#ifndef HELMETSYSTEMCONFIG_H
#define HELMETSYSTEMCONFIG_H

#include "HelmetLEDs.h"

/**************************************************************************************
	Configuration options:
		0 = Choose Primary Osc w/ PLL; in External Crystal mode
		1 = PLL Multiplier x20; PLL Input Divider /2
		2 = System PLL output clk divider /1, Peripheral Clk divider /8
		3 = Watchdog timer disabled
		4 = Clock switching disabled
		
		Define system clock = 8 MHz * 20 / 2 = 80 MHz
**************************************************************************************/
/* 0 */ #pragma config FNOSC = PRIPLL, POSCMOD = XT
/* 1 */ #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2
/* 2 */ #pragma config FPLLODIV = DIV_1, FPBDIV = DIV_8
/* 3 */ #pragma config FWDTEN = OFF
/* 4 */ #pragma config FCKSM = CSDCMD


/**************************************************************************************
	Definitions
**************************************************************************************/
// Define constants relating to config settings
#define	GetSystemClock() 			(80000000ul)
#define	GetPeripheralClock()		(GetSystemClock()/(1 << OSCCONbits.PBDIV))
#define	GetInstructionClock()		(GetSystemClock())

/**************************************************************************************
	Function Prototypes
**************************************************************************************/
void ConfigSystem( void );
void EnableInterrupts( void );

/**************************************************************************************
	Functions
**************************************************************************************/
void ConfigSystem() {

	// Do a little wait (1 sec wait ==> i=1000000 )
	int i = 4000000; // ~4 seconds waiting
	while( i>0 ) i--;
	
	// Configure the device for maximum performance but do not change the PBDIV
	// Given the options, this function will change the flash wait states, RAM
	// wait state and enable prefetch cache but will not change the PBDIV.
	// The PBDIV value is already set via the pragma FPBDIV option above.
	SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
	
	// Disable JTAG debugging
	mJTAGPortEnable( DEBUG_JTAGPORT_OFF ); 	
}

void EnableInterrupts() {
	INTEnableSystemMultiVectoredInt();
    INTEnableInterrupts();
}	

#endif